import { ErrorMessageMode } from '/#/axios';
import { toolsHttp as defHttp } from '/@/utils/http';
import type { SetDeckList, SetDeckQueryParams, GameId, DeckConfigData, LikeParams } from './model/setDeckModel';
import { isArray } from '/@/utils/is';
import { includes } from 'lodash-es';

function InitParams(params: SetDeckQueryParams) {
  const jsonParams: any = {};
  for (const item in params) {
    if (isArray(params[item])) {
      jsonParams[item] = params[item].length ? params[item].join(',') : undefined;
      continue;
    }
    jsonParams[item] = params[item];
  }
  return jsonParams;
}

enum Api {
  SetDeckConfig = '/app/card-deck/config',
  SetDeckList = '/app/card-deck/square-list',
  SetDeckLike = '/app/card-deck/like',
  SetDeckCopy = '/app/card-deck/copy',
}

// 获取套牌配置
export function DeckConfigApi(params: GameId, model: ErrorMessageMode = 'dialog') {
  return defHttp.post<DeckConfigData>(
    {
      url: Api.SetDeckConfig,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 获取套牌列表
export function SetDeckListApi(params: SetDeckQueryParams, model: ErrorMessageMode = 'none') {
  const jsonParams = InitParams(params);
  const searchFormJson: any = {};
  const excludeKeyArr = ['game_id', 'page', 'page_size', 'deck_name', 'tags', 'card_ids'];
  for (const ketStr in jsonParams) {
    if (!includes(excludeKeyArr, ketStr)) {
      searchFormJson[ketStr] = jsonParams[ketStr];
    }
  }
  jsonParams['search_form_json'] = JSON.stringify(searchFormJson);
  return defHttp.post<SetDeckList>(
    {
      url: Api.SetDeckList,
      data: jsonParams,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 赞踩
export function SetDeckLikeApi(params: LikeParams, model: ErrorMessageMode = 'dialog') {
  return defHttp.post(
    {
      url: Api.SetDeckLike,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}

// 复制套牌
export function SetDeckCopyApi(params: Pick<LikeParams, 'deck_id'>, model: ErrorMessageMode = 'dialog') {
  return defHttp.post(
    {
      url: Api.SetDeckCopy,
      data: params,
    },
    {
      errorMessageMode: model,
    }
  );
}
